from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, AuthenticationForm
from .models import Readers


class ReadersChangeForm(forms.ModelForm):
    class Meta:
        model = Readers
        fields = '__all__'


class ReadersCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = Readers
        fields = UserCreationForm.Meta.fields + ('student_number', 'contact_info', 'name')

    class ReadersChangeForm(UserChangeForm):
        class Meta:
            model = Readers
            fields = ('username', 'email', 'student_number', 'contact_info', 'name')


UserModel = get_user_model()


class ReadersAuthenticationForm(AuthenticationForm):
    username = forms.CharField(label="Username", max_length=63,
                               widget=forms.TextInput(attrs={'class': 'form-control',
                                                             'placeholder': 'Username'}))
    password = forms.CharField(label="Password",
                               widget=forms.PasswordInput(attrs={'class': 'form-control',
                                                                 'placeholder': 'Password'}))

    def confirm_login_allowed(self, user):
        if not user.is_active:
            raise forms.ValidationError("This account is currently inactive.")
